<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>日报系统</title>

		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="this is my page">
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link rel="stylesheet" type="text/css"
			href="css/jquery.mobile-1.4.5.min.css">
		<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
		<script type="text/javascript" src="js/jquery.mobile-1.4.5.js"></script>
		<link rel="stylesheet" type="text/css"
			href="css/jquery.mobile-1.4.5.min.css">
		<script type="text/javascript" src="js/jquery.mobile.simpledialog.min.js"></script>
		<script>
	$(function() {
		//1.登陆
		$("#subId").bind('click', function() {
			$.ajax( {
				url : "../admin!appLogin",
				data : {
					userAccount : $("#userNameId").val(),
					pwd : $("#passwordId").val()
				},
				success : function(data) {
					var obj = eval('(' + data + ')');
					if (obj.result == 1) {
						$.mobile.changePage("ribao.html", {
							transition : "slide"
						});
						
						var radioVar = $('input:radio:checked').val();
						if(radioVar==1){
						    dataBase.transaction( function(tx) { 
							tx.executeSql(
							"select * from user", 
							[], 
							function(tx,result){
							     var len = result.rows.length;
							     var recordset = result.rows.item;
							     if(len > 0){
						            updateTab();
							    }else{
							        insertTab();
							    }
							}, 
							function(tx, error){});
							});
						}else{
						   deleteTab();
						}
					} else if (obj.result == 2) {
						//$.mobile.changePage("index.html", {transition: "slide"});
				$("#msgId").html(obj.msg);
			} else {
				$("#msgId").html("网络链接异常!");
			}
		}
			});
		});

		//2.点击用户名和密码框
		$("#userNameId,passwordId").bind('click', function() {
			$("#msgId").html("");
		});
		
		
		//本地数据库操作
		var dataBase = "";
		

		var dataBase = openDatabase("user", "1.0", "用户表", 1024 * 1024, function () { });
		if (!dataBase) {
		    alert("数据库创建失败！");
		} else {
		   createTab();
		}
		
		function createTab(){
		    dataBase.transaction( function(tx) { 
			tx.executeSql(
			"create table if not exists user (id REAL UNIQUE, name TEXT,pwd TEXT)", 
			[], 
			function(tx,result){ }, 
			function(tx, error){ 
			});
			});
		}
		
		
		//插入数据
		function insertTab(){
		    dataBase.transaction( function(tx) { 
			tx.executeSql(
			"insert into user (id, name,pwd) values(1, '"+$("#userNameId").val()+"','"+$("#passwordId").val()+"')", 
			[], 
			function(tx,result){}, 
			function(tx, error){});
			});
		}
		
		//插入数据
		function deleteTab(){
		    dataBase.transaction( function(tx) { 
			tx.executeSql(
			"delete from user where id= 1", 
			[], 
			function(tx,result){}, 
			function(tx, error){});
			});
		}
		
		//查找
		function queryTab(){
		    dataBase.transaction( function(tx) { 
			tx.executeSql(
			"select * from user", 
			[], 
			function(tx,result){
			     var len = result.rows.length;
			     var recordset = result.rows.item;
			     if(len > 0){
		            var id = result.rows.item(0)["id"];
		            var name = result.rows.item(0)["name"];
		            var pwd = result.rows.item(0)["pwd"];
		            $("#userNameId").val(name),
					$("#passwordId").val(pwd)
			    }
			}, 
			function(tx, error){});
			});
		}
		
		//更新数据
		function updateTab(){
		    dataBase.transaction( function(tx) { 
			tx.executeSql(
			"update user set name = '"+$("#userNameId").val()+"' , pwd = '"+$("#passwordId").val()+"' where id= 1", 
			[], 
			function(tx,result){
			}, 
			function(tx, error){});
			});
		}
		
		queryTab();

	});
</script>
	</head>

	<body>
		<div data-role="page" id="indexId" data-title="日报系统">
			<div data-role="content">
				<div>
					<center>
						<h3>
							日报系统v1.0
						</h3>
					</center>
				</div>
				<div data-role="fieldcontain">
					<label for="userName">
						账号:
					</label>
					<input type="text" name="userName" id="userNameId" value=""
						placeholder="请输入账号" />
				</div>
				<div data-role="fieldcontain">
					<label for="password">
						密码:
					</label>
					<input type="password" name="password" id="passwordId" value=""
						placeholder="请输入密码" />
				</div>
				  <br/>
					<div class="ui-grid-b">
						<div class="ui-block-a">
							是否记住密码:
						</div>
						<div class="ui-block-b">
							<div class="ui-grid-c">
							    <div class="ui-block-a">
							        <input type="radio" name="jizhu" id="jizhuId" value="1"
								checked="checked" />
						        </div>
						        <div class="ui-block-b">
						             <span>&nbsp;&nbsp;是</span>
						        </div>
							</div>
						</div>
						<div class="ui-block-c">
							<div class="ui-grid-d">
							    <div class="ui-block-a">
							        <input type="radio" name="jizhu" id="bujizhuId" value="2" />
						        </div>
						        <div class="ui-block-b">
						             <span>&nbsp;&nbsp;&nbsp;否</span>
						        </div>
							</div>
						</div>
					</div>
				<center>
					<div id="msgId" style='color: red;'></div>
				</center>
				<a data-role="button" id="subId">登陆</a>
			</div>
		</div>
	</body>
</html>
